package com.multidisplay.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.multidisplay.bo.ResultBo;
import com.multidisplay.mapper.BaseDataMapper;
import com.multidisplay.po.BaseData;
import com.multidisplay.po.User;
import com.multidisplay.pojo.AddUser;
import com.multidisplay.service.impl.BaseDataServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@CrossOrigin
@Api(tags="存储资源管理相关接口")
public class BaseDataController {
    @Autowired
    private BaseDataServiceImpl baseDataService;
    @Resource
    private BaseDataMapper baseDataMapper;

    @RequestMapping(value = "/basedataadd")
    @ApiOperation("增加BaseData")
    @ResponseBody
    public ResultBo<BaseData> add(BaseData baseData){
        ResultBo<BaseData> resultBo=new ResultBo<>();
        int i=baseDataService.add(baseData);
        if (i==1){
            resultBo.setMsg("success");
            return resultBo;
        }
        else if(i==-1){
            resultBo.setMsg("exist");
            return resultBo;
        }
        else {
            resultBo.setMsg("false");
            return resultBo;
        }
    }

    @PostMapping("/basedatadelById")
    @ApiOperation("根据id删除BaseData")
    public ResultBo<BaseData> delById(int id){
        try {
            baseDataService.delById(id);
            ResultBo<BaseData> resultBo=new ResultBo<>();
            resultBo.setMsg("1");
            return resultBo;
        }catch (Exception e){
            ResultBo<BaseData> resultBo=new ResultBo<>();
            resultBo.setMsg("0");
            return resultBo;
        }
    }

    @GetMapping("/basedataselectById")
    @ApiOperation("根据id查询BaseDatar")
    public List<BaseData> selectById(int id){
        return baseDataService.selectById(id);
    }

    @RequestMapping(value = "/basedataselectByuId")
    @ResponseBody
    public ResultBo<BaseData> selectByuId(@RequestParam(name = "uId",required = false) String uId, int page, int limit){
        if (uId=="") {
            ResultBo<BaseData> resultBo = baseDataService.selectAll(page, limit);
            return resultBo;
        }
        QueryWrapper<BaseData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("u_id",uId);
        IPage iPage = new Page(page, limit);
        IPage<BaseData> baseDataIPage = baseDataMapper.selectPage(iPage, queryWrapper);
        ResultBo<BaseData> resultBo = new ResultBo<>();
        resultBo.setCode(0);
        resultBo.setMsg("");
        resultBo.setCount(baseDataIPage.getTotal());
        resultBo.setData(baseDataIPage.getRecords());
        return resultBo;
    }
    @GetMapping("/basedataselectByName")
    @ApiOperation("根据name查询BaseData")
    public List<BaseData> selectByName(String name){
        return baseDataService.selectByName(name);
    }

    @RequestMapping(value = "/basedataselectAll")
    @ResponseBody
    public ResultBo<BaseData> selectAll(int page,int limit){
        ResultBo<BaseData> resultBos=baseDataService.selectAll(page,limit);
        return resultBos;
    }

    @PutMapping("/basedataupdate")
    @ApiOperation("更新BaseData")
    public void update(BaseData baseData){
        baseDataService.update(baseData);
    }

}
